﻿<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using BusinessObjects.BO_GetObjects;
using DigiLib;

public class Handler : IHttpHandler, System.Web.SessionState.IReadOnlySessionState, System.Web.SessionState.IRequiresSessionState
{
    private string action, data = string.Empty;

    public void ProcessRequest(HttpContext context)
    {
        action = context.Request["action"].ToString();
        data = context.Request["data"].ToString();
        switch (action)
        {
            case "login":
                string[] val = data.Split('|');
                context.Response.Write(Authentication(val[0], val[1], context));
                break;
            case "register":
                context.Response.Write(MemberRegistration(data));
                break;
            case "forgot_password":
                context.Response.Write("forgot_password");
                break;
            case "post_deals":
                if (context.Session["Members"] == null)
                    context.Response.Write(getMessageCode("Thông báo", "Bạn cần đăng nhập để có thể sử dụng chức năng đăng tin khuyến mãi!", 1));
                else
                    context.Response.Write(postDeal(data, context));
                break;
            case "update_deals":
                if (context.Session["Members"] == null)
                    context.Response.Write(getMessageCode("Thông báo", "Bạn cần đăng nhập để có thể sử dụng chức năng đăng tin khuyến mãi!", 1));
                else
                    context.Response.Write(updateDeal(data, context));
                break;
            case "newsletters":
                context.Response.Write("newsletters");
                break;
            case "count":
                GetCount(data);
                break;
            case "deletedeal":
                DeleteDeal(data);
                break;
            default:
                break;
        }
        context.Response.ContentType = "text/plain";
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    protected string DeleteDeal(string id)
    {
        return DigiLib.SqlUtils.ExecuteScalar("Update dbo.portal_deals Set STATUS = 0 WHERE DEAL_ID = " + id + " Select @@Rowcount;").ToString();
    }

    protected int GetCount(string sql)
    {
        return Convert.ToInt32(DigiLib.SqlUtils.ExecuteScalar(sql));
    }

    protected string MemberRegistration(string data)
    {
        string[] datas = data.Split(',');
        int region_id = Convert.ToInt32(datas.GetValue(0));
        string username = datas.GetValue(1).ToString();
        string password = DigiLib.Encrypt.ToMD5(datas.GetValue(2).ToString());
        string avatar = datas.GetValue(3).ToString();
        string fullname = datas.GetValue(4).ToString();
        string email = datas.GetValue(5).ToString();
        string mobile = datas.GetValue(6).ToString();
        bool gender = (Convert.ToInt32(datas.GetValue(7)) == 0) ? true : false;
        string company = datas.GetValue(8).ToString();
        string address = datas.GetValue(9).ToString();
        DateTime birthday = Convert.ToDateTime(datas.GetValue(10));
        string nick = datas.GetValue(11).ToString();
        string website = datas.GetValue(12).ToString();

        return BO_portal_members.Insert(region_id, "", "", username, password, avatar, fullname, email, mobile,
            gender, company, address, birthday, nick, System.Data.SqlTypes.SqlDateTime.Null,
            System.Data.SqlTypes.SqlDateTime.Null, false, false, 0, 0, 0, 0, System.Data.SqlTypes.SqlDateTime.Null,
            System.Data.SqlTypes.SqlDateTime.Null, false, System.Data.SqlTypes.SqlDateTime.Null,
            string.Empty, string.Empty, System.Data.SqlTypes.SqlDateTime.Null, string.Empty,
            string.Empty, DateTime.Now, "MEMBER", System.Data.SqlTypes.SqlDateTime.Null,
            string.Empty, 1);
    }

    protected string Authentication(string username, string password, HttpContext context)
    {
        return BO_portal_members.Login(username, password);
    }

    protected string postDeal(string data, HttpContext context)
    {
        try
        {

            Members objDT = (Members)context.Session["Members"];

            string[] arrData = data.Split(new string[] { ";_*-(-18*****" }, StringSplitOptions.RemoveEmptyEntries);
            int category_id = Convert.ToInt32(arrData[0].Trim());
            int region_id = Convert.ToInt32(arrData[1].Trim());
            Int64 member_id = Convert.ToInt32(arrData[2].Trim()); ;
            if (objDT != null)
            {
                member_id = Convert.ToInt32(objDT.ID);
            }
            int deal_type_id = Convert.ToInt32(arrData[3].Trim()); ;
            string deal_code = "";//arrData[4].Trim();
            string thumb_img = arrData[4].Trim();
            if (thumb_img == "/")
            {
                thumb_img = "";
            }
            string title = arrData[5].Trim();
            string description = arrData[6].Trim();
            string deal_details = arrData[7].Trim();
            string meta_keywords = arrData[8].Trim();
            string meta_description = arrData[9].Trim();
            int total_comments = Convert.ToInt32(arrData[10].Trim());
            DateTime last_comment_date = Convert.ToDateTime(arrData[11].Trim());
            string last_comment_by = "";//arrData[13].Trim();
            int total_vote = Convert.ToInt32(arrData[12].Trim());
            int total_view = Convert.ToInt32(arrData[13].Trim());
            bool is_display = Convert.ToBoolean(arrData[14].Trim());
            bool is_vip = Convert.ToBoolean(arrData[15].Trim());
            DateTime vip_start_date = Convert.ToDateTime(arrData[16].Trim());
            DateTime vip_end_date = Convert.ToDateTime(arrData[17].Trim());
            int order_display = Convert.ToInt32(arrData[18].Trim());
            int order_display_in_category = Convert.ToInt32(arrData[19].Trim());
            decimal origin_price;
            decimal sale_price;
            decimal down_price_in_percent;
            try
            {
                if (arrData[20].Trim() != "Giá gốc")
                    origin_price = Convert.ToDecimal(arrData[20].Trim());
                else
                    origin_price = 0;
                if (arrData[21].Trim() != "Giá khuyến mãi")
                    sale_price = Convert.ToDecimal(arrData[21].Trim());
                else
                    sale_price = 0;
                if (arrData[22].Trim() != "Khuyến mãi")
                    down_price_in_percent = Convert.ToDecimal(arrData[22].Trim());
                else
                    down_price_in_percent = 0;
            }
            catch (Exception)
            {
                return getMessageCode("Thông báo", "Giá tiền và khuyến mãi phải là số!", 1);
            }
            bool is_finish = Convert.ToBoolean(arrData[23].Trim());
            DateTime approved_date = Convert.ToDateTime(arrData[24].Trim());
            string approved_by = "";
            DateTime publish_date = Convert.ToDateTime(arrData[25].Trim());
            DateTime end_publish_date = Convert.ToDateTime(arrData[26].Trim());
            DateTime created_date = Convert.ToDateTime(arrData[27].Trim());
            string created_by = arrData[28].Trim();
            DateTime modify_date = Convert.ToDateTime("1/1/1753");
            string modify_by = "";
            int status = Convert.ToInt32(arrData[29].Trim());

            string strReturn = BusinessObjects.BO_GetObjects.BO_portal_deals.Insert(category_id, region_id, member_id, deal_type_id, deal_code, thumb_img, title, description, deal_details, meta_keywords, meta_description, total_comments, last_comment_date, last_comment_by, total_vote, total_view, is_display, is_vip, vip_start_date, vip_end_date, order_display, order_display_in_category, origin_price, sale_price, down_price_in_percent, is_finish, approved_date, approved_by, publish_date, end_publish_date, created_date, created_by, modify_date, modify_by, status);
            if (strReturn != "0")
            {
                return getMessageCode("Thông báo", "Đăng tin khuyến mãi thành công!", 2);
            }
            return getMessageCode("Thông báo", "Có lỗi trong quá trình gửi tin! Xin thử lại!", 1);
        }
        catch (Exception ex)
        {
            return getMessageCode("Thông báo", "Có lỗi trong quá trình gửi tin! Xin thử lại!" + ex.Message, 1);
        }

    }

    protected string updateDeal(string data, HttpContext context)
    {
        try
        {
            Members objDT = (Members)context.Session["Members"];
            string[] arrData = data.Split(new string[] { ";_*-(-18*****" }, StringSplitOptions.RemoveEmptyEntries);
            int category_id = Convert.ToInt32(arrData[0].Trim());
            int region_id = Convert.ToInt32(arrData[1].Trim());
            Int64 member_id = Convert.ToInt32(arrData[2].Trim()); ;
            if (objDT != null)
            {
                member_id = Convert.ToInt32(objDT.ID);
            }
            int deal_type_id = Convert.ToInt32(arrData[3].Trim()); ;
            string deal_code = "";//arrData[4].Trim();
            string thumb_img = arrData[4].Trim();
            if (thumb_img == "/")
            {
                thumb_img = "";
            }
            string title = arrData[5].Trim();
            string description = arrData[6].Trim();
            string deal_details = arrData[7].Trim();
            string meta_keywords = arrData[8].Trim();
            string meta_description = arrData[9].Trim();
            int total_comments = Convert.ToInt32(arrData[10].Trim());
            DateTime last_comment_date = Convert.ToDateTime(arrData[11].Trim());
            string last_comment_by = "";//arrData[13].Trim();
            int total_vote = Convert.ToInt32(arrData[12].Trim());
            int total_view = Convert.ToInt32(arrData[13].Trim());
            bool is_display = Convert.ToBoolean(arrData[14].Trim());
            bool is_vip = Convert.ToBoolean(arrData[15].Trim());
            DateTime vip_start_date = Convert.ToDateTime(arrData[16].Trim());
            DateTime vip_end_date = Convert.ToDateTime(arrData[17].Trim());
            int order_display = Convert.ToInt32(arrData[18].Trim());
            int order_display_in_category = Convert.ToInt32(arrData[19].Trim());
            decimal origin_price;
            decimal sale_price;
            decimal down_price_in_percent;
            try
            {
                if (arrData[20].Trim() != "Giá gốc")
                    origin_price = Convert.ToDecimal(arrData[20].Trim());
                else
                    origin_price = 0;
                if (arrData[21].Trim() != "Giá khuyến mãi")
                    sale_price = Convert.ToDecimal(arrData[21].Trim());
                else
                    sale_price = 0;
                if (arrData[22].Trim() != "Khuyến mãi")
                    down_price_in_percent = Convert.ToDecimal(arrData[22].Trim());
                else
                    down_price_in_percent = 0;
            }
            catch (Exception)
            {
                return getMessageCode("Thông báo", "Giá tiền và khuyến mãi phải là số!", 1);
            }
            bool is_finish = Convert.ToBoolean(arrData[23].Trim());
            DateTime approved_date = Convert.ToDateTime(arrData[24].Trim());
            string approved_by = "";
            DateTime publish_date = Convert.ToDateTime(arrData[25].Trim());
            DateTime end_publish_date = Convert.ToDateTime(arrData[26].Trim());
            DateTime created_date = Convert.ToDateTime(arrData[27].Trim());
            string created_by = arrData[28].Trim();
            DateTime modify_date = Convert.ToDateTime("1/1/1753");
            string modify_by = "";
            int status = Convert.ToInt32(arrData[29].Trim());
            int id = Convert.ToInt32(arrData[30].Trim());
            string strReturn = BusinessObjects.BO_GetObjects.BO_portal_deals.Update(id, category_id, region_id, member_id, deal_type_id, deal_code, thumb_img, title, description, deal_details, meta_keywords, meta_description, total_comments, last_comment_date, last_comment_by, total_vote, total_view, is_display, is_vip, vip_start_date, vip_end_date, order_display, order_display_in_category, origin_price, sale_price, down_price_in_percent, is_finish, approved_date, approved_by, publish_date, end_publish_date, created_date, created_by, modify_date, modify_by, status);
            if (strReturn != "0")
            {
                return getMessageCode("Thông báo", "Cập nhật khuyến mãi thành công!", 2);
            }
            return getMessageCode("Thông báo", "Có lỗi trong quá trình gửi tin! Xin thử lại!", 1);
        }
        catch (Exception ex)
        {
            return getMessageCode("Thông báo", "Có lỗi trong quá trình gửi tin! Xin thử lại!" + ex.Message, 1);
        }

    }

    protected string getMessageCode(string title, string message, int status)
    {
        if (status == 1)
            return "<div id=\"divMessageBox\" class=\"successful_registration\" style=\"padding:0;position:relative;top:0\"><div class=\"notice_error\">    <img src=\"images/error.png\">    <h6>        " + title + "</h6>    <p>        " + message + "</p></div> </div>";
        else
            return "<div id=\"divMessageBox\" class=\"successful_registration\" style=\"padding:0;position:relative;top:0\"><div class=\"notice\">    <img src=\"images/success.png\">    <h6>        " + title + "</h6>    <p>        " + message + "</p></div> </div>";
    }

}